package com.zy.cat.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import com.zy.cat.service.impl.CacheServiceImpl;

import lombok.extern.slf4j.Slf4j;

public class DateAPIUtils {
	public final static String YYYYMMDD = "yyyy-MM-dd";
	public final static String HHMMSS = "hh:mm:ss";
	public final static String YEAR_MONTH_DAY = "yyyy年MM月dd日";
	public final static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
	public final static String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
	public final static String YYYYMMDDHHMMSS_NOSIGN = "yyyyMMddHHmmss";
	public final static String YYYYMMDD_1 = "yyyyMMdd";

	// ---------------------------- Formatter ---------------------------

	/**
	 * 2018-05-31
	 */
	public final static DateTimeFormatter FORMATTER_LOCAL = DateTimeFormatter.ISO_LOCAL_DATE;

	/**
	 * 2018年05月31日
	 */
	public final static DateTimeFormatter FORMATTER_YEAR_MONTH_DAY = DateTimeFormatter.ofPattern(YEAR_MONTH_DAY);

	/**
	 * 2018-05-31 11:12:42
	 */
	public final static DateTimeFormatter FORMATTER_YYYYMMDDHHMMSS = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS);

	/**
	 * 20180531110806694
	 */
	public final static DateTimeFormatter FORMATTER_YYYYMMDDHHMMSSSSS = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSSSSS);

	/**
	 * 20180629173455
	 */
	public final static DateTimeFormatter FORMATTER_YYYYMMDDHHMMSS_NOSIGN = DateTimeFormatter
			.ofPattern(YYYYMMDDHHMMSS_NOSIGN);

	/**
	 * 2018-05-31
	 */
	public final static DateTimeFormatter FORMATTER_YYYYMMDD = DateTimeFormatter.ofPattern(YYYYMMDD);

	public final static DateTimeFormatter FORMATTER_YYYYMMDD_1 = DateTimeFormatter.ofPattern(YYYYMMDD_1);

	/**
	 * 获取当前时间，并格式化成给定格式
	 *
	 * @param formatter
	 * @return
	 */
	public static String getNowTimeString(DateTimeFormatter formatter) {
		LocalDateTime now = LocalDateTime.now();
		return now.format(formatter);
	}

	/**
	 * 获取当前时间Unix格式 (10位int)
	 *
	 * @return
	 */
	public static Integer getNowTimeUnix() {
		LocalDateTime now = LocalDateTime.now();
		return (int) now.toEpochSecond(ZoneOffset.of("+8"));
	}

	/**
	 * Unix时间戳格式化成String
	 *
	 * @param unixTime
	 * @param formatter
	 * @return
	 */
	public static String unixToString(int unixTime, DateTimeFormatter formatter) {
		Instant instant = Instant.ofEpochSecond(unixTime);
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.of("+8"));
		return localDateTime.format(formatter);
	}

	/**
	 * String格式时间转换成Unix时间戳
	 *
	 * @param timeStr   "yyyy-MM-dd HH:mm:ss" 这种格式
	 * @param formatter
	 * @return
	 */
	public static int stringToUnix(String timeStr) {
		if (timeStr.length() == 10) {
			timeStr = timeStr + " 00:00:00";
		}
		LocalDateTime localDateTime = LocalDateTime.parse(timeStr, FORMATTER_YYYYMMDDHHMMSS);
		return (int) localDateTime.toEpochSecond(ZoneOffset.of("+8"));
	}

	/**
	 * String格式时间转换成Unix时间戳
	 *
	 * @param timeStr   "yyyy-MM-dd" 这种格式
	 * @param formatter
	 * @return
	 */
	public static int stringToUnix(String timeStr, DateTimeFormatter formatter) {
		if (timeStr.length() == 10) {
			timeStr = timeStr + " 00:00:00";
		}
		LocalDateTime localDateTime = LocalDateTime.parse(timeStr, FORMATTER_YYYYMMDDHHMMSS);
		return (int) localDateTime.toEpochSecond(ZoneOffset.of("+8"));
	}

	/**
	 * 获得时间开始 例：传入 2015-12-03 返回 2015-12-03 00:00:00
	 *
	 * @param curDate
	 * @return
	 */
	public static String getDayStart(String curDate) {
		return curDate + " 00:00:00";
	}

	/**
	 * 获得时间结束 例：传入 2015-12-03 返回 2015-12-03 23:59:59
	 *
	 * @param curDate
	 * @return
	 */
	public static String getDayEnd(String curDate) {
		return curDate + " 23:59:59";
	}

	/**
	 * 获取秒值
	 *
	 * @return
	 */
	public static Integer getMyTimeInMillis() {
		// 当前时间
		long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() / 1000;
		return (int) milliseconds;
	}

	/**
	 * 时间戳 去掉时分秒
	 * 
	 * @param unixTime
	 * @return
	 */
	public static Integer getYYYYMMDDUnix(Integer unixTime) {
		Instant instant = Instant.ofEpochSecond(unixTime);
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.of("+8"));
		return DateAPIUtils.stringToUnix(localDateTime.format(DateAPIUtils.FORMATTER_YYYYMMDD));
	}

	/**
	 * string转Date类型
	 * 
	 * @param param
	 * @param format
	 * @return
	 */
	public static Date stringToDate(String source, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			Date date = format.parse(source);
			return date;
		} catch (ParseException e) {
			//时间转换错误
		}
		return null;
	}
	
	/**
	 * Date转string类型
	 * 
	 * @param param
	 * @param format
	 * @return
	 */
	public static String dateToString(Date source, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(source);
	}

	/**
	 * 判断字符串是否为指定格式
	 * @author lipengchao
	 * @date 2019-12-10 10:34:55
	 * @param source
	 * @param pattern
	 * @return
	 */
	public static Boolean isFormat(String source,String pattern) {
		SimpleDateFormat format=new SimpleDateFormat(pattern);
		try {
			format.parse(source);
			return true;
		} catch (ParseException e) {
//			e.printStackTrace();
		}
		return false;
	}
}
